<?php


namespace App\Services;


use App\Models\HouseList;
use App\Tools\RedisTools;
use App\Tools\TxdtTools;

class RedisGeo
{
    public $redis;

    public function __construct()
    {
        $this->redis = (RedisTools::getInstance())->connectRedis();
    }

    /**
     * @Notes:经纬度存入geo
     * @Author:Planb,
     * @Date: 2022/11/11,
     * @Time: 13:51,
     */
    public function setHouseLocation($city, $district, $location, $houseId, $type)
    {
        //位置转化经纬
        $address = (new TxdtTools())->addressName($city . $district . $location);
        $addressArray = explode(',', $address);
        //维度
        $latitude = $addressArray[0];
        //经度
        $longitude = $addressArray[1];
        //redis geo存储经纬度信息  先经度 后维度
        $this->redis->geoadd("$city:$district:$type", $longitude, $latitude, $houseId);
    }

    /**
     * @Notes:获取附近的房源信息 房源名称 + 经纬度
     * @Author:Planb,
     * @Date: 2022/11/11,
     * @Time: 14:54,
     */
    public function getNearbyHouse($city, $district, $location)
    {
        //位置转化经纬度
        $address = (new TxdtTools())->addressName($city . $district . $location);
        $addressArray = explode(',', $address);
        //维度
        $latitude = $addressArray[0];
        //经度
        $longitude = $addressArray[1];
        //redis georadius  withcoord  返回位置名称和经纬度
        $data = $this->redis->georadius("$city:$district", $longitude, $latitude, '100', 'km', ['withcoord']);
        $result = [];
        foreach ($data as $k => $v) {
            $result[$k]['house_name'] = $v[0];
            //经度
            $result[$k]['longitude'] = $v[1][0];
            //维度
            $result[$k]['latitude'] = $v[1][1];
        }
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * @Notes:我的附近房源
     * @Author:Planb,
     * @Date: 2022/11/22,
     * @Time: 11:00,
     */
    public function mapFindHouse($latitude, $longitude, $distance, $type, $price)
    {
        $address = $latitude . ',' . $longitude;
        $result = (new TxdtTools())->listAddress($address);
        //城市
        $city = $result['city'];
        //区
        $district = $result['district'];
        $house = $this->redis->georadius("$city:$district:$type", $longitude, $latitude, $distance, 'm', ['withcoord']);
        foreach ($house as $k => $v) {
            $houseInfo = HouseList::where('id', $v[0])->whereBetween('house_price', [$price['min'], $price['max']])->first();
            if (!$houseInfo) {
                $addressResult = [];
                continue;
            } else {
                //房屋id
                $addressResult[$k]['id'] = intval($v[0]);
                //经度
                $addressResult[$k]['longitude'] = $v[1][0];
                //维度
                $addressResult[$k]['latitude'] = $v[1][1];
                //markers点宽
                $addressResult[$k]['width'] = 30;
                //markers点高
                $addressResult[$k]['height'] = 40;
                $addressResult[$k]['label'] = [
                    //房源名称
                    'content' => $houseInfo['house_name']
                ];
            }
        }
        if ($addressResult) {
            return $addressResult;
        } else {
            return false;
        }
    }
}
